<?php
/* 
 *采购询价
 */
class Ask_supplierAction extends CommonAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
       if(!empty($key)) {
            $where['Product.name'] = array('like',"%".$key."%");//产品名称
            $where['Product.ename'] = array('like',"%".$key."%");
            $where['Product.cas'] = array('like',"%".$key."%");//条码
            $where['Ask_supplier.mano'] = array('like',"%".$key."%");//条码
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        
        if($_REQUEST['status'] != '' )
            $map['Ask_supplier.status'] = $_REQUEST['status'];

        if($_REQUEST['charge_man'] != '' )
            $map['Ask_supplier.charge_man'] = $_REQUEST['charge_man'];

        if(!empty($_REQUEST['back_supplier_id'])){
           $map['back_supplier_id'] = $_REQUEST['back_supplier_id'];
        }

        if(!empty($_REQUEST['inquiry_mano']))
            $map['Inquiry.mano'] = array('like',"%".$_REQUEST['inquiry_mano']."%");

    }

    public function _before_edit(){
        $this->assign('offers',M('Ask_supplier_detail')->where('ask_supplier_id = '.$_REQUEST['id']) ->order('update_time desc')->findAll() );
        $pid = getModelName($_REQUEST['id'],'Ask_supplier','product_id');
        import ( "@.ORG.Page" );
        $assign_data = array();
        //历史询价
        $map_his_offfers = 'ask_supplier_id != '.$_REQUEST['id'] .' and product_id = '.$pid;
        $count_his_offers = M('Ask_supplier_detail')->where($map_his_offfers) ->count(); 
        if($count_his_offers > 0){
            $page0 = new Page($count_his_offers,25);
            $his_offers = M('Ask_supplier_detail')->where($map_his_offfers)->limit($page0->firstRow.','.$page0->listRows)->order('update_time desc')->findAll(); 
            $assign_data = array(
                'his_offers' => $his_offers,
                'page0' => $page0->show(),
            ); 
        }

        $ask_supplier = M('Ask_supplier')->getById( $_REQUEST['id'] ); $map = 'product_id = '.$ask_supplier['product_id' ] ;


        $count = M('Buy')->where($map)->count();
        if($count > 0){
            $page = new Page($count,25);
            $ps = M('Buy')->where($map)->limit($page->firstRow.','.$page->listRows)->order('update_time desc')->findAll(); 
            $assign_data['products'] = $ps;
            $assign_data['page'] = $page->show();
        }

        $count_supplier = M('Supplier_product')->where($map)->count();
        if($count_supplier > 0){
            $page2 = new Page($count,25);
            $ps2 = M('Supplier_product')->where($map)->limit($page2->firstRow.','.$page2->listRows)->order('sort desc')->findAll(); 
            $assign_data2 = array(
                'suppliers' => $ps2,
                'page2' => $page2->show(),
            );
            $assign_data = array_merge($assign_data,$assign_data2); 
        }

        $this->assign($assign_data);
    }

    public function  index() {
        if(!empty($_REQUEST['inquiry_mano']))
            parent::index('Ask_supplierInquiryView');
        else
            parent::index('Ask_supplierView');
    }

    public function _before_insert(){
        if(!empty($_POST['inquiry_id']))
        if($rs = M('Ask_supplier')->where('inquiry_id = '.$_POST['inquiry_id'])->getField('id') ){
           $this->error('存在重复的询价单！请不用再新增询价单！');
        }

    }	
    
   public function _before_update()
    {
        if( $_POST['inquiry_id'] >0  )
            M('Inquiry')->where('id = '.$_POST['inquiry_id'])->setField('status',$_POST['status']);
    }

    //分配责任人
    public function charge()
    {
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $list = M('Ask_supplier') ->where ( $condition )->findAll();
            $sale_ids = array(); //有效的SALE_ID
            if(!is_array($list))
                $this->error('您选择的采购询价单不存在！');
            $this->assign('receives',$list);
            $this->display();
        }else
          $this->error("请选择采购询价单！");
    }

     //目前暂用于批量分配负责人
    public function mass_update()
    {
         $data = $_POST;
        $count_no = count($data['charge_man']);
        for($i =0; $i < $count_no;$i++) {
          $_POST['id'] =  $data['id'][$i];
        //批量指定负责人
        if(!empty($data['charge_man'][$i]))
            $_POST['charge_man'] = $data['charge_man'][$i];
        
            $this->micro_update($_POST['id']);

          unset($_POST);
        }

        $jumpUrl = $_REQUEST['jumpUrl'];
        if(empty($jumpUrl))
            $jumpUrl = Cookie::get ( '_currentUrl_' );
        $this->assign ( 'jumpUrl', $jumpUrl );
        $this->success ('批量修改成功!');
    }

}
?>
